package com.questionnaire.modular.system.controller;

import com.questionnaire.common.base.R;
import com.questionnaire.common.base.ShiroUser;
import com.questionnaire.constant.ShiroConstant;
import com.questionnaire.modular.system.model.MenuNode;
import com.questionnaire.modular.system.service.MenuService;
import com.questionnaire.utils.ApiMenuFilter;
import com.questionnaire.utils.ShiroKit;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: YSL
 * @time: 2023/12/15 14:16
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;
//    用户对应资源菜单
    @PostMapping("/menu")
    public R test(HttpServletRequest request){
        R<Object> result = new R<>();
        Session session1 = ShiroKit.getSession();
        ShiroUser user1 = ShiroKit.getUser();
        System.out.println(user1.getId()+user1.getUserAuth());
        //            获取用户角色
        List<Integer> roleList = ShiroKit.getUser().getRoleList();
//            根据角色获取前端菜单资源
        List<MenuNode> menus = menuService.getMenusByRoleIds(roleList);
        List<String> menuNameList = new ArrayList<String>();
        for (MenuNode menuNode:menus){
            menuNameList.add(menuNode.getName());
        }
//            组成 菜单+子菜单
        List<MenuNode> titles = MenuNode.buildTitle(menus);
        titles = ApiMenuFilter.build(titles);
        result = R.success(ShiroConstant.LOGIN_SUCCESS_CODE, ShiroConstant.LOGIN_SUCCESS_MESSAGE,titles,"");
        return result;
    }
}
